﻿using SqlSugar;
using VM.Common.MD5Until;
using VM.Entity.EntityMap.Token;
using VM.Entity.EntityMap.User;
using VM.IBLL.User;
using VM.IDAL;

namespace VM.BLL.User
{
    public class UserBLL : BaseBLL,  IUserBLL
    {

        private readonly ISqlSuggarDAL _ISqlSuggarDAL;
        public UserBLL(ISqlSugarClient client, ISqlSuggarDAL sqlSuggarDAL)
        {
            _ISqlSuggarDAL = sqlSuggarDAL;
        }

        /// <summary>
        /// 检查用户名、密码、公钥是否存在
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public CheckUserInfoResult CheckUserInfo(CheckUser obj)
        {
            //md5加密
            obj.PassWord = MD5ED.MD5Encrypt32(obj.PassWord);
            List<Sys_Users> resultList = _ISqlSuggarDAL.Query<Sys_Users>(e => e.PublicKey == obj.PublicKey
                                                                && e.Phone == obj.Phone
                                                                && e.PassWord == obj.PassWord
                                                          ).ToList();
            CheckUserInfoResult result = new CheckUserInfoResult();
            int Res = 500;
            string PrivateKey = "";
            if (resultList.Count == 1)
            {
                result.UserId = resultList[0].Id;
                result.status = 200;
                result.Msg = "校验成功";
                result.PrivateKey = resultList[0].PrivateKey;
            }
            else if (resultList.Count > 1)
            {
                result.UserId = 0;
                result.status = 500;
                result.Msg = "存在多条";
                result.PrivateKey = "";

            }
            else
            {
                result.UserId = 0;
                result.status = 500;
                result.Msg = "用户名、密码或公钥提供错误";
                result.PrivateKey = "";
            }
            return result;
        }

        /// <summary>
        /// 校验用户名和密码是否正确
        /// </summary>
        /// <param name="PublicKey"></param>
        /// <param name="PrivateKey"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool CheckUserKeys(string RefreshKey, out string Msg)
        {
            int Countx = _ISqlSuggarDAL.Query<Sys_RefreshToken>(e => e.RefreshKey == RefreshKey
                                                                  && e.EndDateTimes >= DateTime.Now
                                                            ).ToList().Count();
            Msg = Countx.ToString();
            if (Countx >= 1)
            {
                return true;

            }
            else
            {
                return false;
            }


        }

        /// <summary>
        /// 添加RefreshToken值
        /// </summary>
        /// <param name="obj"></param>
        public void AddRefreshKeys(Sys_RefreshToken obj)
        {
            _ISqlSuggarDAL.InsertAsync<Sys_RefreshToken>(obj);
        }
    }
}